<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <!-- 用户名11111111111111111111111111111111111111 -->
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="请输入用户名">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <!-- 密码项目111111111111111111111111 -->
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" v-model="param.password">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <!-- 验证码项11111111111111111111111111 -->
                <el-form-item prop="code">
                    <el-row>
                        <el-col :span="19">
                            <el-input placeholder="请输入验证码" v-model="param.code">
                                <el-button class="iconfont el-icon-mobile-phone" slot="prepend"></el-button>
                            </el-input>
                        </el-col>
                        <el-col :span="4">
                            <img :src="imgCode" alt="" @click="changeCode" class="imagesCode" />
                        </el-col>
                    </el-row>
                </el-form-item>
                <!-- 登陆按钮111111111111111111111111111111 -->
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import axios from 'axios';
import Qs from 'qs';

export default {
    data() {
        return {
            imgCode: this.$myUrl.BASEURL + 'login/kaptcha?jjj=' + Math.random(),
            param: {
                username: 'admin',
                password: '123456',
                code: ''
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'change' },
                    { min: 4, max: 12, message: '用户名长度为6-12个字符', trigger: 'change' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'change' },
                    { min: 6, max: 18, message: '密码长度为6-18个字符', trigger: 'change' }
                ],
                code: [{ required: true, min: 4, max: 4, message: '请输入4位数的验证码', trigger: 'change' }]
            }
        };
    },
    created() {
        this.ImagesCode();
    },
    methods: {
        changeCode() {
            this.ImagesCode();
        },
        ImagesCode() {
            this.imgCode = this.$myUrl.BASEURL + 'login/kaptcha?jjj=' + Math.random();
        },
        submitForm() {
            
            axios({
                url: this.$myUrl.BASEURL + 'login/login',
                method: 'post',
                headers: { withCredentials: true },
                data: Qs.stringify({
                    userName: this.param.username,
                    password: this.param.password,
                    imgCode: this.param.code
                })
            }).then((res) => {
                console.log(res);
                if (res.data.code === '1002') {
                    this.$message.error('验证码错误!');
                } else if (res.data.code === '1001') {
                    this.$message.error('用户名或密码错误!');
                } else {
                    this.$message.success('登录成功');
                    this.$router.push('/dashboard');
                }
            });
            // console.log(sessionid);
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
.imagesCode {
    width: 130%;
}
</style>
